package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.Link;
import com.example.demo.model.Node;
import com.example.demo.service.RoutingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import javax.servlet.annotation.MultipartConfig;
import java.util.Map;

/**
 * @author ：Docin
 * @date ：Created in 2020/9/11 10:46
 * @description：路由算法
 * @modified By：
 */

@Controller
@RequestMapping(value="/routing")
@CrossOrigin
@MultipartConfig
public class RoutingController {

    private Node[] nodeslist;

    //常量
    final static int NO_CONNECTION = -1;
    final static int F_ASYM = 0;
    final static int B_ASYM = 1;
    final static int SYM = 2;

    @Autowired
    RoutingService routingService;

    //http://localhost:9876/routing/allnode
    @RequestMapping(value = "/allnode")
    @ResponseBody
//    @CrossOrigin
    public List<Node> nodelist() {
        List<Node> nodes = routingService.allNodes();
        return nodes;
    }


    @RequestMapping(value = "/allterminal")
    @ResponseBody
//    @CrossOrigin
    public String termlist(@RequestParam(required = false, defaultValue = "1") int page,
                           @RequestParam(required = false, defaultValue = "10") int limit) {
        List<Node> terms = routingService.queryterminal(page, limit);
        int count = routingService.queryterminalCount();
        JSONObject jo = new JSONObject();
        jo.put("code", 0);
        jo.put("msg", "");
        jo.put("count", count);
        jo.put("data", terms);
        return jo.toString();
    }

    @RequestMapping(value = "/classLevel")
    @ResponseBody
//    @CrossOrigin
    //班编制的情况
    public List<Node> classResponse(@RequestParam("id[]") Integer[] id){
        System.out.println(id[1]);
        List<Node> tmpNodes = routingService.getsomeNodes(id);
        Node[] nodes = new Node[id.length];
        tmpNodes.toArray(nodes);
        Node[] monitors = new Node[2];
        monitors = routingService.election(nodes);
        for (int i = 0; i < id.length; i++) {
            if (tmpNodes.get(i).getId() == monitors[0].getId()){
                Node tmpnd = tmpNodes.get(i);
                tmpNodes.remove(i);
                tmpNodes.add(0,tmpnd);
            }
            else if (tmpNodes.get(i).getId() == monitors[1].getId()){
                Node tmpnd = tmpNodes.get(i);
                tmpNodes.remove(i);
                tmpNodes.add(1,tmpnd);
            }
        }

        return tmpNodes;
    }

    @RequestMapping(value = "/platoonLevel")
    @ResponseBody
//    @CrossOrigin
    //排编制的情况
    public Map<String,Object> platoonResponse(@RequestParam("id[]")  Integer[] id){
        List<Node> tmpNodes = routingService.getsomeNodes(id);
        Node[] nodes = new Node[id.length];
        tmpNodes.toArray(nodes);

        this.nodeslist = new Node[nodes.length];
        this.nodeslist = nodes;

        routingService.initRout(nodes); //初始化路由表
        routingService.getNeibNodes(nodes);
        for (int i = 0; i < id.length; i++)
            routingService.broadcast(nodes[i]);
        routingService.routing(nodes);
        List<Link> linkF_SYM = new ArrayList<Link>();
        List<Link> linkSYM = new ArrayList<Link>();
        int cnt = 0;
        for (int i = 0; i < id.length; i++) {
            for (int j = 0; j < nodes[i].neibNodes.size(); j++) {
                if (nodes[i].neibNodes.get(j).getLinkstate() == F_ASYM){
                    Link lk = new Link(cnt, nodes[i].getId(), nodes[i].neibNodes.get(j).getNode().getId());
                    linkF_SYM.add(lk);
                    cnt = cnt + 1;
                }
                else if (nodes[i].neibNodes.get(j).getLinkstate() == SYM){
                    if (nodes[i].getId() > nodes[i].neibNodes.get(j).getNode().getId()) continue;
                    Link lk = new Link(cnt, nodes[i].getId(), nodes[i].neibNodes.get(j).getNode().getId());
                    linkSYM.add(lk);
                    cnt = cnt + 1;
                }
            }
        }
        Map<String, Object> linkdata = new HashMap<String, Object>();
        linkdata.put("SYM", linkSYM);
        linkdata.put("F_SYM", linkF_SYM);
        return linkdata;
    }

    @RequestMapping(value = "/showRouter")
    @ResponseBody
//    @CrossOrigin
    public int[] showRout(@RequestParam Integer src, @RequestParam Integer dst){
        for (int i = 0; i < this.nodeslist.length; i++) {
            if (this.nodeslist[i].getId() == src){
                for (int j = 0; j < this.nodeslist[i].routers.length; j++) {
                    if (this.nodeslist[i].routers[j].getDstId() == dst && this.nodeslist[i].routers[j].getNextId() != -1){
                        if (this.nodeslist[i].routers[j].getNextId() == dst){
                            int[] link = {src, dst};
                            return link;
                        }
                        else if (this.nodeslist[i].routers[j].getNextId() != dst){
                            int[] link = {src, this.nodeslist[i].routers[j].getNextId(), dst};
                            return link;
                        }
                    }
                }
            }
        }
        return new int[]{-1};
    }







}